﻿<?php
// set_time_limit(0); 
// $conn = mysql_connect("localhost", "root", "") or die("Can not connect mysql!");
// mysqli_select_db($conn,"alizi") or die("database not exist!");
// mysqli_query($conn,"set names 'utf8'");
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "alizi";
 
// 创建连接
// global $conn;
 $conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
mysqli_query($conn,"set names utf8"); 
$sql = "SELECT `country_id`,`name` FROM ali_country WHERE country_id>255";
$sql1 = "SELECT a.country_id,az.`name`,az.zone_id FROM ali_country a LEFT JOIN ali_zone az ON a.country_id=az.country_id WHERE a.country_id>255";
$result = mysqli_query($conn, $sql);
// $result1 = mysqli_query($conn, $sql1);
 function diqu(){
	   if(mysqli_num_rows($result) > 0) {
			// $res=mysqli_fetch_all($result);
			// var_dump(json_encode($res));
			// 输出数据
			$json='';
			while($row = mysqli_fetch_assoc($result)) {
				// echo "id: " . $row["country_id"]. " - Name: " . $row["name"]. "<br>";
				$json.=$row["country_id"].':'."'".$row["name"]."',";
			}
			file_put_contents('json.txt',$json);
			var_dump($json);
		} else {
			echo "0 结果";
		}
	 
 }

$country=array(256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277);
getshijson($country,$conn); 
function getshijson($country,$conn){
	foreach($country as $coun){
		$sql1 = "SELECT a.country_id,az.`name`,az.zone_id FROM ali_country a LEFT JOIN ali_zone az ON a.country_id=az.country_id WHERE a.country_id='$coun'";
		
		$result1 = mysqli_query($conn, $sql1);
		 dishi($result1);
	}
	
}
function dishi($result1){
	   if(mysqli_num_rows($result1) > 0) {
			// $res=mysqli_fetch_all($result);
			// var_dump(json_encode($res));
			// 输出数据
			$json='';
			while($row = mysqli_fetch_assoc($result1)) {
				// echo "id: " . $row["country_id"]. " - Name: " . $row["name"]. "<br>";
				$json.=$row["zone_id"].':'."'".$row["name"]."',";
			}
			// file_put_contents('json.txt',$json);
			file_put_contents('ww.txt',$json.PHP_EOL,FILE_APPEND );
			var_dump($json);
		} else {
			echo "0 结果";
		}
	 
 }

 
mysqli_close($conn);
?>